﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Admin_Admin_Download : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitMenu();
        if (!IsPostBack)
        {
            OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
            var downloads = from download in dc.Downloads
                            orderby download.Time descending
                            select new
                            {
                                标识 = download.Download_id,
                                标题 = download.Download_name,
                                日期 = download.Time.Value.Month + "-" + download.Time.Value.Day
                            };
            DownloadGridView.DataSource = downloads;
            DownloadGridView.DataBind();
            dc.Dispose();
        }

    }

    protected void InitMenu()
    {

        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
        Menu1.Items.Clear();
        MenuItem MainPage = new MenuItem();
        MainPage.Enabled = true;
        MainPage.Selectable = true;
        MainPage.Selected = false;
        MainPage.Value = MainPage.ToolTip = MainPage.Text = "注销登陆";
        MainPage.NavigateUrl = "../Default.aspx";
        Menu1.Items.Add(MainPage);

        MenuItem navigation = new MenuItem();
        navigation.Value = navigation.ToolTip = navigation.Text = "页眉管理";
        navigation.NavigateUrl = "Admin_navigation.aspx";
        Menu1.Items.Add(navigation);

        MenuItem news = new MenuItem();
        news.Value = news.ToolTip = news.Text = "重要通知";
        Menu1.Items.Add(news);

        MenuItem catergory1 = new MenuItem();//定义子菜单
        catergory1.Text = "类别管理";
        catergory1.NavigateUrl = "Admin_DocumentCategory.aspx";
        Menu1.Items[2].ChildItems.Add(catergory1);//添加子菜单

        MenuItem aa1 = new MenuItem();
        aa1.Text = "重要通知管理";
        aa1.NavigateUrl = "Admin_Notice.aspx";
        Menu1.Items[2].ChildItems.Add(aa1);


        MenuItem document = new MenuItem();
        document.Value = document.ToolTip = document.Text = "校发文件";
        Menu1.Items.Add(document);

        MenuItem catergory2 = new MenuItem();//定义子菜单
        catergory2.Text = "类别管理";
        catergory2.NavigateUrl = "Admin_DocumentCategory.aspx";
        Menu1.Items[3].ChildItems.Add(catergory2);//添加子菜单

        MenuItem aa2 = new MenuItem();
        aa2.Text = "校发文件管理";
        aa2.NavigateUrl = "Admin_Document.aspx";
        Menu1.Items[3].ChildItems.Add(aa2);





        int a = 0;

        var Menus = from menu in dc.Menus
                    select new
                    {
                        MenuItem_id = menu.MenuItem_id,
                        MenuItem_Name = menu.MenuItem_Name
                    };

        foreach (var menu in Menus)
        {
            MenuItem menuitem = new MenuItem();
            menuitem.Enabled = true;
            menuitem.Selectable = true;
            menuitem.Selected = false;
            menuitem.ToolTip = menuitem.Text = menu.MenuItem_Name.Trim();
            menuitem.Value = menu.MenuItem_id.ToString();
            Menu1.Items.Add(menuitem);
            a++;
            MenuItem catergory = new MenuItem();//定义子菜单
            catergory.Text = "类别管理";
            catergory.NavigateUrl = "Admin_DocumentCategory.aspx";
            Menu1.Items[a + 3].ChildItems.Add(catergory);//添加子菜单

            MenuItem aa = new MenuItem();

            aa.Text = menu.MenuItem_Name.ToString();


            aa.Text = aa.Text.Trim() + "管理";
            Menu1.Items[a + 3].ChildItems.Add(aa);

        }
        Menu1.Items[4].ChildItems[0].NavigateUrl = "";
        Menu1.Items[4].ChildItems[1].NavigateUrl = "";
        Menu1.Items[5].ChildItems[1].NavigateUrl = "Admin_Orgnization.aspx";
        Menu1.Items[6].ChildItems[1].NavigateUrl = "Admin_Rule.aspx";
        Menu1.Items[7].ChildItems[1].NavigateUrl = "Admin_Meeting.aspx";
        Menu1.Items[8].ChildItems[1].NavigateUrl = "Admin_CollegeCondition.aspx";
        Menu1.Items[9].ChildItems[1].NavigateUrl = "Admin_Law.aspx";
        Menu1.Items[10].ChildItems[1].NavigateUrl = "Admin_Download.aspx";







        MenuItem link = new MenuItem();
        link.Value = link.ToolTip = link.Text = "链接管理";
        link.NavigateUrl = "Admin_Link.aspx";
        Menu1.Items.Add(link);

        MenuItem password = new MenuItem();
        password.Value = password.ToolTip = password.Text = "密码管理";
        password.NavigateUrl = "Admin_Password.aspx";
        Menu1.Items.Add(password);


        dc.Dispose();
    }

    
    protected void MotifyDownload_Click(object sender, EventArgs e)
    {
        Motify_DownloadName.Visible = false;
        MotifyDownload.Visible = false;
        Cancle_modify.Visible = false;

        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
        var motify_downloads = from download in dc.Downloads
                        where download.Download_id.Equals(Convert.ToInt32(Motify_DownloadID.Text.Trim()))
                        select download;
        if (!motify_downloads.ToArray().ElementAt(0).Download_name.Equals(Motify_DownloadName.Text.Trim()))
        {
            motify_downloads.ToArray().ElementAt(0).Download_name = Motify_DownloadName.Text.Trim();
            dc.SubmitChanges();
        }
        
        var downloads = from download in dc.Downloads
                        orderby download.Time descending
                        select new
                        {
                            标识 = download.Download_id,
                            标题 = download.Download_name,
                            日期 = download.Time.Value.Month + "-" + download.Time.Value.Day
                        };
        DownloadGridView.DataSource = downloads;
        DownloadGridView.DataBind();
        dc.Dispose();

    }
    protected void Upload_Click(object sender, EventArgs e)
    {
        Download_name.Visible = false;
        FileUpLoad.Visible = false;
        Upload.Visible = false;
        New_name.Visible = false;
        New_choose.Visible = false;
        Cancle_upload.Visible = false;

        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
        if (Download_name.Text.Trim().Length != 0)
        {

            if (FileUpLoad.HasFile)
            {
                string fileContentType = FileUpLoad.PostedFile.ContentType;

                FileInfo File = new FileInfo(FileUpLoad.PostedFile.FileName);
                string FileName = File.Name;
                string WebFilePath = Server.MapPath("../Download/" + FileName);
                try
                {
                    FileUpLoad.SaveAs(WebFilePath);
                }
                catch (Exception ex)
                {
                    JavaScript.JSDialog.Alert(this, "提示：文件上传失败，失败原因：" + ex.Message);
                }

                Download download = new Download();
                download.Download_name = Download_name.Text.Trim();
                download.Time = DateTime.Now;
                download.URL = "~/Download/" + FileName;

                SQL.SQLHelper.Insert(download);
                dc.SubmitChanges();
            }
            else
            {
                JavaScript.JSDialog.Alert(this, "上传文件不能为空！");
            }
        }
        else 
        {
            JavaScript.JSDialog.Alert(this, "下载名不能为空！");
        }

        var downloads = from download in dc.Downloads
                        orderby download.Time descending
                        select new
                        {
                            标识 = download.Download_id,
                            标题 = download.Download_name,
                            日期 = download.Time.Value.Month + "-" + download.Time.Value.Day
                        };
        DownloadGridView.DataSource = downloads;
        DownloadGridView.DataBind();
        dc.Dispose();
        Download_name.Text = "";

    }
    protected void DownloadGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();

        var delete_download = from download in dc.Downloads
                              where download.Download_id.Equals(Convert.ToInt32(((Label)DownloadGridView.Rows[e.RowIndex].FindControl("News_id")).Text))
                              select download;

        try
        {
            dc.Downloads.DeleteAllOnSubmit(delete_download);
            dc.SubmitChanges();
        }
        catch (Exception ex)
        {
        }


        var downloads = from download in dc.Downloads
                        orderby download.Time descending
                        select new
                        {
                            标识 = download.Download_id,
                            标题 = download.Download_name,
                            日期 = download.Time.Value.Month + "-" + download.Time.Value.Day
                        };
        DownloadGridView.DataSource = downloads;
        DownloadGridView.DataBind();
        dc.Dispose();
    }
    protected void DownloadGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Motify_DownloadName.Visible = true;
        MotifyDownload.Visible = true;
        Cancle_modify.Visible = true;
        Motify_DownloadID.Text = ((Label)DownloadGridView.Rows[e.NewEditIndex].FindControl("News_id")).Text.Trim();
        Motify_DownloadName.Text = ((Label)DownloadGridView.Rows[e.NewEditIndex].FindControl("News_Title")).Text.Trim();
    }
    protected void UploadButton_Click(object sender, EventArgs e)
    {
        Download_name.Visible = true;
        FileUpLoad.Visible = true;
        Upload.Visible = true;
        New_name.Visible = true;
        New_choose.Visible = true;
        Cancle_upload.Visible = true;
    }
    protected void Cancle_modify_Click(object sender, EventArgs e)
    {
        Motify_DownloadName.Visible = false;
        MotifyDownload.Visible = false;
        Cancle_modify.Visible = false;
    }
    protected void Cancle_upload_Click(object sender, EventArgs e)
    {
        Download_name.Visible = false;
        FileUpLoad.Visible = false;
        Upload.Visible = false;
        New_name.Visible = false;
        New_choose.Visible = false;
        Cancle_upload.Visible = false;
    }
    protected void DownloadGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        DownloadGridView.PageIndex = e.NewPageIndex;
        DataBind(); //重新绑定GridView数据的函数
    }
}

